﻿Imports System.Xml
Imports System.Reflection
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Drawing.Printing

Module mdlHerramientas
    'Configuración conexión a base de datos Postgres
    Public idconprueba As New Odbc.OdbcConnection '("Driver={PostgreSQL ANSI};Server=127.0.0.1;Port=5432;Database=dbwsin;Uid=postgres;Pwd=complex")
    Public strDriver As String = "{PostgreSQL ANSI}"
    Public strServer As String = ""     '"127.0.0.1"
    Public strPort As String = ""       '"5432"
    Public strDatabase As String = ""   '"dbwsin"
    Public strDatabasews As String = ""
    Public strUid As String = ""    '"postgres"
    Public strPwd As String = ""    '"complex"
    Public boolTurnero As Boolean = False
    Public boolCabecera As Boolean = False

    'Configuración de Acceso a Servicio Web y a Software de Autenticación Sirius
    Public strRutaSirius As String = ""
    Public intNumintentos As Integer = 3
    Public strUrl As String = ""
    'Public strUsuario As String = ""
    'Public strPassword As String = ""

    Public strRutafotografia As String = ""
    Public strRutahuelladerecha As String = ""
    Public strRutahuellaizquierda As String = ""

    Public strCrcNombre As String = ""
    Public strCrcRazon As String = ""
    Public strCrcTelefono As String = ""
    Public strCrcDireccion As String = ""
    Public strCrcNit As String = ""
    Public strCrcRutalogo As String = ""
    Public strCrcRutalogoAlt As String = ""
    Public strCrcResolucion As String = ""
    Public strCrcOnac As String = ""
    Public strCrcCodigoPre As String = ""
    Public strCrcEncabezado As String = ""

    Public Sub probarConexion(ByVal strObjServer As String, ByVal strObjPort As String, ByVal strObjDatabase As String, ByVal strObjUid As String, ByVal strObjPwd As String)
        Try
            mdlHerramientas.idconprueba.ConnectionString = "Driver=" & strDriver & ";Server=" & strObjServer & ";Port=" & strObjPort & ";Database=" & strObjDatabase & ";Uid=" & strObjUid & ";Pwd=" & strObjPwd
            mdlHerramientas.idconprueba.Open()

            MsgBox("Conexión exitosa", MsgBoxStyle.Information, "Conexión a base de datos")
            mdlHerramientas.idconprueba.Close()
        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "Conexión a base de datos")
        End Try
    End Sub

    Public Sub loadOption()
        Dim xmlOptions As XmlDocument = New XmlDocument()
        Dim ndNode As XmlNode
        Dim ndSubNode As XmlNode
        Dim xmlPath As String = Application.StartupPath & "\xml"
        Dim xmlFolder As Boolean

        xmlFolder = Directory.Exists(xmlPath)

        If xmlFolder = False Then
            Directory.CreateDirectory(xmlPath)
        End If

        Try
            xmlOptions.Load(xmlPath & "\ConfigOpt.xml")
            ndNode = xmlOptions.DocumentElement

            For Each ndNode In ndNode.ChildNodes
                For Each ndSubNode In ndNode.ChildNodes
                    Select Case ndSubNode.Name
                        Case "rutaSirius"
                            mdlHerramientas.strRutaSirius = ndSubNode.InnerText.ToString
                        Case "numIntentos"
                            mdlHerramientas.intNumintentos = ndSubNode.InnerText.ToString
                        Case "url"
                            mdlHerramientas.strUrl = ndSubNode.InnerText.ToString
                        Case "usuario"
                            mdlIntegracion.athUsuario = ndSubNode.InnerText.ToString
                        Case "password"
                            mdlIntegracion.athContrasenia = ndSubNode.InnerText.ToString
                        Case "Server"
                            mdlHerramientas.strServer = ndSubNode.InnerText.ToString
                        Case "Port"
                            mdlHerramientas.strPort = ndSubNode.InnerText.ToString
                        Case "Uid"
                            mdlHerramientas.strUid = ndSubNode.InnerText.ToString
                        Case "Pwd"
                            mdlHerramientas.strPwd = ndSubNode.InnerText.ToString
                        Case "Turnero"
                            mdlHerramientas.boolTurnero = ndSubNode.InnerText.ToString
                        Case "Cabecera"
                            mdlHerramientas.boolCabecera = ndSubNode.InnerText.ToString
                        Case "webservice"
                            mdlHerramientas.strDatabasews = ndSubNode.InnerText.ToString
                        Case "crc"
                            mdlHerramientas.strDatabase = ndSubNode.InnerText.ToString
                        Case "Fotografia"
                            mdlHerramientas.strRutafotografia = ndSubNode.InnerText.ToString
                        Case "Huelladerecha"
                            mdlHerramientas.strRutahuelladerecha = ndSubNode.InnerText.ToString
                        Case "Huellaizquierda"
                            mdlHerramientas.strRutahuellaizquierda = ndSubNode.InnerText.ToString
                        Case "CrcNombre"
                            mdlHerramientas.strCrcNombre = ndSubNode.InnerText.ToString
                        Case "CrcRazon"
                            mdlHerramientas.strCrcRazon = ndSubNode.InnerText.ToString
                        Case "CrcTelefono"
                            mdlHerramientas.strCrcTelefono = ndSubNode.InnerText.ToString
                        Case "CrcDireccion"
                            mdlHerramientas.strCrcDireccion = ndSubNode.InnerText.ToString
                        Case "CrcNit"
                            mdlHerramientas.strCrcNit = ndSubNode.InnerText.ToString
                        Case "CrcLogo"
                            mdlHerramientas.strCrcRutalogo = ndSubNode.InnerText.ToString
                        Case "CrcLogoAlt"
                            mdlHerramientas.strCrcRutalogoAlt = ndSubNode.InnerText.ToString
                        Case "CrcResolucion"
                            mdlHerramientas.strCrcResolucion = ndSubNode.InnerText.ToString
                        Case "CrcOnac"
                            mdlHerramientas.strCrcOnac = ndSubNode.InnerText.ToString
                        Case "CrcCodigoPre"
                            mdlHerramientas.strCrcCodigoPre = ndSubNode.InnerText.ToString
                        Case "CrcEncabezado"
                            mdlHerramientas.strCrcEncabezado = ndSubNode.InnerText.ToString
                    End Select
                Next
            Next

        Catch ex As Exception
            Dim objControl As Object
            Dim File As Object
            Dim i As Integer = 0

            objControl = CreateObject("Scripting.FileSystemObject")
            File = objControl.CreateTextFile(xmlPath & "\ConfigOpt.xml")
            File.WriteLine("<?xml version='1.0' encoding='utf-8' ?>")
            File.WriteLine("<options>")
            File.WriteLine("<shell>")
            File.WriteLine("<rutaSirius></rutaSirius>")
            File.WriteLine("<numIntentos></numIntentos>")
            File.WriteLine("</shell>")
            File.WriteLine("")
            File.WriteLine("<webservice>")
            File.WriteLine("<url></url>")
            File.WriteLine("<usuario></usuario>")
            File.WriteLine("<password></password>")
            File.WriteLine("</webservice>")
            File.WriteLine("")
            File.WriteLine("<dbConexion>")
            File.WriteLine("<Driver>{PostgreSQL UNICODE}</Driver>")
            File.WriteLine("<Server>127.0.0.1</Server>")
            File.WriteLine("<Port>5432</Port>")
            File.WriteLine("<Uid></Uid>")
            File.WriteLine("<Pwd></Pwd>")
            File.WriteLine("</dbConexion>")
            File.WriteLine("")
            File.WriteLine("<dbBaseDatos>")
            File.WriteLine("<crc></crc>")
            File.WriteLine("<webservice></webservice>")
            File.WriteLine("</dbBaseDatos>")
            File.WriteLine("")
            File.WriteLine("<rutasImagenes>")
            File.WriteLine("<Fotografia></Fotografia>")
            File.WriteLine("<Huelladerecha></Huelladerecha>")
            File.WriteLine("<Huellaizquierda></Huellaizquierda>")
            File.WriteLine("</rutasImagenes>")
            File.WriteLine("<otros>")
            File.WriteLine("<Turnero></Turnero>")
            File.WriteLine("<Cabecera></Cabecera>")
            File.WriteLine("<CrcNombre></CrcNombre>")
            File.WriteLine("<CrcRazon></CrcRazon>")
            File.WriteLine("<CrcTelefono></CrcTelefono>")
            File.WriteLine("<CrcDireccion></CrcDireccion>")
            File.WriteLine("<CrcNit></CrcNit>")
            File.WriteLine("<CrcLogo></CrcLogo>")
            File.WriteLine("<CrcLogoAlt></CrcLogoAlt>")
            File.WriteLine("<CrcResolucion></CrcResolucion>")
            File.WriteLine("<CrcOnac></CrcOnac>")
            File.WriteLine("<CrcCodigoPre></CrcCodigoPre>")
            File.WriteLine("<CrcEncabezado></CrcEncabezado>")
            File.WriteLine("</otros>")
            File.WriteLine("</options>")
            File.Close()
        End Try
    End Sub

    Public Sub saveOption(ByVal frmFom As frmOpciones)
        Dim xmlOptions As XmlDocument = New XmlDocument()
        Dim ndNode As XmlNode
        Dim ndSubNode As XmlNode
        Dim xmlPath As String = Application.StartupPath & "\xml"
        Dim xmlFolder As Boolean
        Dim rutaLogo As String

        xmlFolder = Directory.Exists(xmlPath)

        If xmlFolder = False Then
            Directory.CreateDirectory(xmlPath)
        End If

        xmlOptions.Load(xmlPath & "\ConfigOpt.xml")
        ndNode = xmlOptions.DocumentElement
        For Each ndNode In ndNode.ChildNodes
            For Each ndSubNode In ndNode.ChildNodes
                Select Case ndSubNode.Name
                    Case "rutaSirius"
                        ndSubNode.InnerText = frmFom.txtExaminarShell.Text
                    Case "numIntentos"
                        ndSubNode.InnerText = frmFom.nudNumintentos.Value
                    Case "url"
                        ndSubNode.InnerText = frmFom.txtURL.Text
                    Case "usuario"
                        ndSubNode.InnerText = frmFom.txtUsuariows.Text
                    Case "password"
                        ndSubNode.InnerText = frmFom.txtPasswordws.Text
                    Case "Server"
                        ndSubNode.InnerText = frmFom.txtServidor.Text
                    Case "Port"
                        ndSubNode.InnerText = frmFom.txtPuerto.Text
                    Case "Uid"
                        ndSubNode.InnerText = frmFom.txtUsuario.Text
                    Case "Pwd"
                        ndSubNode.InnerText = frmFom.txtContrasenia.Text
                    Case "Turnero"
                        ndSubNode.InnerText = frmFom.chkTurnero.Checked
                    Case "Cabecera"
                        ndSubNode.InnerText = frmFom.chkCabecera.Checked
                    Case "crc"
                        ndSubNode.InnerText = frmFom.txtBasedatos.Text
                    Case "webservice"
                        ndSubNode.InnerText = frmFom.txtBasedatosws.Text
                    Case "Fotografia"
                        ndSubNode.InnerText = frmFom.txtFotografia.Text
                    Case "Huelladerecha"
                        ndSubNode.InnerText = frmFom.txtHuelladerecha.Text
                    Case "Huellaizquierda"
                        ndSubNode.InnerText = frmFom.txtHuellaizquierda.Text
                    Case "CrcNombre"
                        ndSubNode.InnerText = frmFom.txtNombre.Text
                    Case "CrcRazon"
                        ndSubNode.InnerText = frmFom.txtRazon.Text
                    Case "CrcTelefono"
                        ndSubNode.InnerText = frmFom.txtTelefono.Text
                    Case "CrcDireccion"
                        ndSubNode.InnerText = frmFom.txtDireccion.Text
                    Case "CrcNit"
                        ndSubNode.InnerText = frmFom.txtNit.Text
                    Case "CrcLogo"
                        ndSubNode.InnerText = frmFom.txtLogo.Text
                        rutaLogo = Path.Combine(Application.StartupPath, "img\logo.jpg")

                        Try
                            My.Computer.FileSystem.CopyFile(
                               frmFom.txtLogo.Text,
                               Path.Combine(rutaLogo),
                               Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
                               Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
                            ndSubNode.InnerText = rutaLogo
                        Catch ex As Exception
                            'MsgBox("Archivo ")
                        End Try
                    Case "CrcLogoAlt"
                        ndSubNode.InnerText = frmFom.txtLogoAlt.Text
                        rutaLogo = Path.Combine(Application.StartupPath, "img\logo_onac.jpg")

                        Try
                            My.Computer.FileSystem.CopyFile(
                                frmFom.txtLogoAlt.Text,
                                Path.Combine(rutaLogo),
                                Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
                                Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
                            ndSubNode.InnerText = rutaLogo
                        Catch ex As Exception
                            'MsgBox("Archivo ")
                        End Try
                    Case "CrcResolucion"
                        ndSubNode.InnerText = frmFom.txtResolucion.Text
                    Case "CrcOnac"
                        ndSubNode.InnerText = frmFom.txtOnac.Text
                    Case "CrcCodigoPre"
                        ndSubNode.InnerText = frmFom.txtCodigoPre.Text
                    Case "CrcEncabezado"
                        ndSubNode.InnerText = frmFom.txtEncabezado.Text
                End Select
            Next
        Next

        xmlOptions.Save(xmlPath & "\ConfigOpt.xml")
    End Sub

    Public Function LeerTxt(ByVal ruta As String) As ArrayList
        Dim objReader As New StreamReader(ruta)
        Dim sLine As String = ""
        Dim arrText As New ArrayList()

        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                arrText.Add(sLine)
            End If
        Loop Until sLine Is Nothing
        objReader.Close()

        'For Each sLine In arrText
        '    Console.WriteLine(sLine)
        'Next
        Return arrText
    End Function

    Public Function LeerMDB(ByVal ruta As String) As ArrayList
        Dim arrText As New ArrayList()
        Dim consulta As String
        Dim idcon As New Odbc.OdbcConnection
        Dim array1, array2 As Array
        Dim adaptar As Odbc.OdbcDataAdapter
        Try
            Dim strStringconn As String = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & ruta & ";UID=;PWD="
            idcon.ConnectionString = strStringconn
            idcon.Open()
            consulta = "SELECT * FROM TestResultsLeft WHERE PatNo='1036599475'"
            adaptar = New Odbc.OdbcDataAdapter(consulta, idcon)
            Dim registro As New DataSet

            adaptar.Fill(registro, "TestResultsLeft")
            array1 = registro.Tables("TestResultsLeft").Rows(0).ItemArray.ToArray
            arrText.Add(array1)

            consulta = "SELECT * FROM TestResultsRight WHERE PatNo='1036599475'"
            adaptar = New Odbc.OdbcDataAdapter(consulta, idcon)

            adaptar.Fill(registro, "TestResultsRight")
            array2 = registro.Tables("TestResultsRight").Rows(0).ItemArray.ToArray
            arrText.Add(array2)




        Catch ex As Exception
            MsgBox("Ha ocurrido un error con la base de datos")
        End Try
        Return arrText
    End Function

    Public Function tipoarchivo(ByVal ruta As String) As Integer
        Dim arr1 As Array
        Dim arr2 As Array
        Dim tipo As Integer = 0 ' 1{text} 2{mdb}
        arr1 = Split(ruta, "\")
        For i = 0 To arr1.Length - 1
            arr2 = Split(arr1(i), ".")
            If (arr2.Length > 0) Then
                For j = 0 To arr2.Length - 1
                    If arr2(j) = "txt" Then
                        tipo = 1
                    End If
                    If arr2(j) = "dat" Then
                        tipo = 1
                    End If
                    If arr2(j) = "mdb" Then
                        tipo = 2
                    End If
                Next j
            End If
        Next i
        Return tipo
    End Function

    Function captureScreen(ByVal locX As Integer, ByVal locY As Integer, ByVal width As Integer, ByVal height As Integer) As Bitmap
        Dim NewImage As New Bitmap(width, height)
        Using g As Graphics = Graphics.FromImage(NewImage)
            g.CopyFromScreen(locX, locY, 0, 0, NewImage.Size)
        End Using
        Return NewImage
    End Function

    Sub SavePicturebox()
        ' Dim bit As Bitmap = captureScreen(PictureBox1.Location.X, PictureBox1.Location.Y, PictureBox1.Width, PictureBox1.Height)
        'bit.Save("C:\fileName.jpg", Imaging.ImageFormat.Jpeg)
    End Sub

    'save the whole form as jpeg format
    Sub SaveWholeForm(ByVal forma As Form)
        Dim bit As Bitmap = captureScreen(forma.Location.X, forma.Location.Y, forma.Width, forma.Height)
        bit.Save("C:\Users\jortiz\Documents\poryecto src\fileName.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub
End Module
